/*
 * Created by JFormDesigner on Tue Nov 20 14:53:30 CST 2018
 */

package cn.xo68.ide.plugin.ui.form;

import cn.xo68.ide.plugin.common.domain.GitSetting;
import cn.xo68.ide.plugin.common.domain.ProjectSetting;
import cn.xo68.ide.plugin.common.service.Settings;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author wyy
 */
public class PluginConfigable implements Configurable {

    private JPanel mainPanel;
    private boolean modified=false;

    public PluginConfigable() {
        mainPanel = new JPanel();
        initComponents();

        ProjectSetting projectSetting=Settings.getInstance().getProjectSetting();
        tfGroupId.setText(projectSetting.getDefaultGroupId());
        tfArtifactId.setText(projectSetting.getDefaultArtifactId());
        tfVersion.setText(projectSetting.getDefaultVersion());
        tfPackages.setText(projectSetting.getDefaultPackages());
        tfAuthor.setText(projectSetting.getDefaultAuthor());

        GitSetting gitSetting = Settings.getInstance().getGitSetting();
        tfGitUrl.setText(gitSetting.getGitRepository());
        tfGitUserName.setText(gitSetting.getUserName());
        tfGitPassword.setText(gitSetting.getPassword());
    }

    private Container getContentPane(){
        return mainPanel;
    }
    private void pack(){

    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label8 = new JLabel();
        tfGroupId = new JTextField();
        label7 = new JLabel();
        tfArtifactId = new JTextField();
        label6 = new JLabel();
        tfVersion = new JTextField();
        label5 = new JLabel();
        tfPackages = new JTextField();
        label4 = new JLabel();
        tfAuthor = new JTextField();
        label1 = new JLabel();
        tfGitUrl = new JTextField();
        label2 = new JLabel();
        tfGitUserName = new JTextField();
        label3 = new JLabel();
        tfGitPassword = new JTextField();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(new GridBagLayout());
        ((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {97, 0, 0};
        ((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0};
        ((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 1.0, 1.0E-4};
        ((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label8 ----
        label8.setText("\u9ed8\u8ba4GroupId");
        contentPane.add(label8, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfGroupId, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label7 ----
        label7.setText("\u9ed8\u8ba4ArtifactId");
        contentPane.add(label7, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfArtifactId, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label6 ----
        label6.setText("\u9ed8\u8ba4Version");
        contentPane.add(label6, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfVersion, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label5 ----
        label5.setText("\u9ed8\u8ba4\u5305\u540d");
        contentPane.add(label5, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfPackages, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label4 ----
        label4.setText("\u9ed8\u8ba4\u4f5c\u8005");
        contentPane.add(label4, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfAuthor, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label1 ----
        label1.setText("\u6570\u636e\u4ed3\u5e93git");
        contentPane.add(label1, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfGitUrl, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label2 ----
        label2.setText("git\u7528\u6237\u540d");
        contentPane.add(label2, new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 5, 5), 0, 0));
        contentPane.add(tfGitUserName, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 0), 0, 0));

        //---- label3 ----
        label3.setText("git\u5bc6\u7801");
        contentPane.add(label3, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.VERTICAL,
            new Insets(0, 0, 0, 5), 0, 0));
        contentPane.add(tfGitPassword, new GridBagConstraints(1, 7, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        pack();
        //setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label8;
    private JTextField tfGroupId;
    private JLabel label7;
    private JTextField tfArtifactId;
    private JLabel label6;
    private JTextField tfVersion;
    private JLabel label5;
    private JTextField tfPackages;
    private JLabel label4;
    private JTextField tfAuthor;
    private JLabel label1;
    private JTextField tfGitUrl;
    private JLabel label2;
    private JTextField tfGitUserName;
    private JLabel label3;
    private JTextField tfGitPassword;
    // JFormDesigner - End of variables declaration  //GEN-END:variables


    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public String getDisplayName() {
        return "ToolSuite";
    }

    @Nullable
    @Override
    public JComponent createComponent() {
        return mainPanel;
    }

    @Override
    public boolean isModified() {
        ProjectSetting projectSetting=Settings.getInstance().getProjectSetting();
        GitSetting gitSetting = Settings.getInstance().getGitSetting();
        return !tfGroupId.getText().equals(projectSetting.getDefaultGroupId())
                || !tfArtifactId.getText().equals(projectSetting.getDefaultArtifactId())
                || !tfVersion.getText().equals(projectSetting.getDefaultVersion())
                || !tfPackages.getText().equals(projectSetting.getDefaultPackages())
                || !tfAuthor.getText().equals(projectSetting.getDefaultAuthor())
                || !tfGitUrl.getText().equals(gitSetting.getGitRepository())
                || !tfGitUserName.getText().equals(gitSetting.getUserName())
                || !tfGitPassword.getText().equals(gitSetting.getPassword());
    }

    @Override
    public void apply() throws ConfigurationException {
        ProjectSetting projectSetting=Settings.getInstance().getProjectSetting();
        projectSetting.setDefaultGroupId(tfGroupId.getText());
        projectSetting.setDefaultArtifactId(tfArtifactId.getText());
        projectSetting.setDefaultVersion(tfVersion.getText());
        projectSetting.setDefaultPackages(tfPackages.getText());
        projectSetting.setDefaultAuthor(tfAuthor.getText());

        Settings.getInstance().setProjectSetting(projectSetting);
        Settings.getInstance().setGitSetting(new GitSetting(tfGitUrl.getText(), tfGitUserName.getText(), tfGitPassword.getText()));

    }
}
